// hooks写法
// import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { login } from '@/api/user'
import type { loginForm } from '@/api/user/type'
import { baseRouter } from '@/router/baseRouter'

export const useUserStore = defineStore('user', () => {
  const token = ref(localStorage.getItem('token'))
  //   const doubleCount = computed(() => count.value * 2)
  const menuRoutse = ref(baseRouter)
  const userLogin = async (data: loginForm) => {
    const result = await login(data)
    if (result.code === 200) {
      console.log(result)
      token.value = result.data.token as string
      localStorage.setItem('token', token.value as string)
      return 'ok'
    } else {
      return Promise.reject(new Error(result.data.message as string))
    }
  }

  return {
    userLogin,
    menuRoutse,
  }
})
